<template>
  <div class="container-box">
    <el-dialog
      :title="title"
      :visible.sync="visible"
      width="80%"
      append-to-body
      custom-class="import-dialog"
    >
      <div class="header">
        <div class="title">
          <!-- 房间：广州市天河区天河路1210号红枫小区B栋2单元2102房 -->
          房间：{{ generateHouseName(houseData) }}
        </div>
      </div>
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        label-width="120px"
        class="demo-form"
      >
        <div class="section">
          <div class="label">
            <label class="dot"> 房间展示 </label>
            <div class="tips">(可上传图片共9张，默认第一张为主图)</div>
          </div>
        </div>
        <el-form-item prop="apartmentPicture">
          <image-upload v-model="form.apartmentPicture" :limit="9" />
        </el-form-item>
        <div class="section">
          <div class="label">
            <label>时间信息</label>
          </div>
          <el-form-item label="周期类型" prop="apartmentType">
            <el-radio-group v-model="form.apartmentType">
              <el-radio label="1">短租</el-radio>
              <el-radio label="2">长租</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="起租时长" prop="rentStartingTime">
            <el-input
              v-model="form.rentStartingTime"
              placeholder="请输入"
              type="number"
              style="width: 220px; margin-right: 5px"
            ></el-input
            >天
          </el-form-item>
          <el-form-item label="可租时间" required>
            <el-col :span="6">
              <el-form-item prop="rentableStartTime">
                <el-date-picker
                  type="date"
                  placeholder="选择日期"
                  v-model="form.rentableStartTime"
                  style="width: 100%"
                  value-format="yyyy-MM-dd"
                  :picker-options="rentableStartTimeOptions"
                ></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col class="line" :span="2">至</el-col>
            <el-col :span="6">
              <el-form-item prop="rentableEndTime">
                <el-date-picker
                  type="date"
                  placeholder="选择日期"
                  v-model="form.rentableEndTime"
                  style="width: 100%"
                  value-format="yyyy-MM-dd"
                  :picker-options="rentableEndTimeOptions"
                ></el-date-picker>
              </el-form-item>
            </el-col>
          </el-form-item>
          <el-form-item label="不可租时间" required>
            <el-col :span="6">
              <el-form-item prop="noRentableStartTime">
                <el-date-picker
                  type="date"
                  placeholder="选择日期"
                  v-model="form.noRentableStartTime"
                  style="width: 100%"
                  value-format="yyyy-MM-dd"
                  :picker-options="noRentableStartTimeOptions"
                ></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col class="line" :span="2">至</el-col>
            <el-col :span="6">
              <el-form-item prop="noRentableEndTime">
                <el-date-picker
                  type="date"
                  placeholder="选择日期"
                  v-model="form.noRentableEndTime"
                  style="width: 100%"
                  value-format="yyyy-MM-dd"
                  :picker-options="noRentableEndTimeOptions"
                ></el-date-picker>
              </el-form-item>
            </el-col>
          </el-form-item>
          <div v-if="form.apartmentType == 1">
            <el-form-item label="可入住时间" prop="checkoutTime">
              <el-time-select
                v-model="form.checkoutTime"
                :picker-options="{
                  start: '00:00',
                  step: '00:15',
                  end: '23:45',
                }"
                placeholder="选择时间"
              >
              </el-time-select>
            </el-form-item>
            <el-form-item label="退租截止时间" prop="leaseEndTime">
              <el-time-select
                v-model="form.leaseEndTime"
                :picker-options="{
                  start: '00:00',
                  step: '00:15',
                  end: '23:45',
                }"
                placeholder="选择时间"
              >
              </el-time-select>
            </el-form-item>
          </div>
          <div v-if="form.apartmentType == 2">
            <el-form-item label="间隔时间" prop="intervalTime">
              <el-input
                v-model="form.intervalTime"
                placeholder="请输入"
                type="number"
                style="width: 220px; margin-right: 5px"
              ></el-input
              >天
            </el-form-item>
            <el-form-item label="退租提醒时间" prop="leaseRemindTime">
              提前
              <el-input
                v-model="form.leaseRemindTime"
                placeholder="请输入"
                type="number"
                style="width: 220px; margin: 0 5px"
              ></el-input
              >天
            </el-form-item>
          </div>
        </div>

        <div class="section">
          <div class="label">
            <label>费用信息</label>
          </div>
          <div v-if="form.apartmentType == 1">
            <el-form-item label="收费类型" prop="chargeType">
              <el-radio-group v-model="form.chargeType">
                <el-radio label="0">固定收费</el-radio>
                <el-radio label="1">时效收费</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item
              label="收费标准"
              prop="priceMap"
              v-if="form.chargeType == 1"
            >
              <div class="facilities">
                <!-- <i class="icon el-icon-circle-plus" @click="addPriceMap"></i> -->
                <div class="flex align-center expense">
                  <i class="icon el-icon-circle-plus" @click="addPriceMap"></i>
                  <div class="price-handle" @click.stop="showCalendarPrice">
                    价格日历
                  </div>
                  <!-- <div
                    class="flex align-center calendar"
                    @click="showCalendar = true"
                  >
                    <i class="icon el-icon-date"></i>查看
                  </div> -->
                </div>
                <el-row
                  :gutter="10"
                  class=""
                  v-for="(item, index) in priceMap"
                  :key="index"
                  style="margin-bottom: 10px"
                >
                  <el-col :span="8">
                    <div class="facilities-item">
                      <el-select
                        v-model="item.timeRangeId"
                        placeholder="请选择"
                      >
                        <el-option
                          v-for="item in rangeList"
                          :key="item.timeRangeId"
                          :label="item.name"
                          :value="item.timeRangeId"
                        >
                        </el-option>
                      </el-select>
                    </div>
                  </el-col>
                  <el-col :span="8">
                    <div class="facilities-item">
                      <el-input
                        v-input.num_point
                        placeholder="请输入金额"
                        v-model="item.fixedPrice"
                        style="width: 220px"
                      >
                      </el-input>
                      <span style="margin: 0 10px">元</span>
                    </div>
                  </el-col>
                  <el-col :span="2">
                    <i
                      class="icon el-icon-remove"
                      @click="removePriceMap(index)"
                    ></i>
                  </el-col>
                </el-row>
              </div>
            </el-form-item>
          </div>

          <div v-if="form.apartmentType == 1">
            <el-form-item
              label="固定租金"
              prop="fixedPrice"
              v-if="form.chargeType == 0"
            >
              <el-input
                v-model="form.fixedPrice"
                type="number"
                placeholder="请输入"
                style="width: 220px; margin-right: 5px"
              ></el-input
              >元
            </el-form-item>
            <el-form-item
              label="日租金"
              prop="averageDaily"
              v-if="form.chargeType == 1"
            >
              <el-input
                v-model="form.averageDaily"
                type="number"
                placeholder="请输入"
                style="width: 220px; margin-right: 5px"
              ></el-input
              >元
            </el-form-item>
          </div>

          <div v-if="form.apartmentType == 2">
            <el-form-item label="月租金" prop="monthPrice">
              <el-input
                v-model="form.monthPrice"
                type="number"
                placeholder="请输入"
                style="width: 220px; margin-right: 5px"
              ></el-input
              >元/月
            </el-form-item>
            <el-form-item label="押付方式" prop="payTypeId">
              <el-select
                v-model="form.payTypeId"
                placeholder="请选择"
                style="width: 220px"
              >
                <el-option
                  v-for="(item, index) in payTypeList"
                  :key="index"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </div>

          <el-form-item label="押金" prop="cashPledge">
            <el-input
              v-model="form.cashPledge"
              type="number"
              placeholder="请输入"
              style="width: 220px; margin-right: 5px"
            ></el-input
            >元
          </el-form-item>
          <el-form-item label="服务费" prop="servicePrice">
            <el-input
              v-model="form.servicePrice"
              type="number"
              placeholder="请输入"
              style="width: 220px; margin-right: 5px"
            ></el-input
            >元/次
          </el-form-item>
          <!-- <el-form-item label="保洁费" prop="baojie">
          <el-input
            v-model="form.baojie"
            type="number"
            placeholder="请输入"
            style="width: 220px; margin-right: 5px"
          ></el-input
          >元
        </el-form-item> -->
          <el-form-item label="退订规则" prop="unsubscribeRuleId">
            <el-input
              v-model="this.unsubscribeRuleData.unsubscribeRuleName"
              placeholder="请选择退订规则"
              readonly
              style="width: 220px; margin-right: 10px"
            ></el-input>
            <el-button
              size="mini"
              type="primary"
              @click="dialogSelectRuleVisible = true"
              >选择退订规则</el-button
            >
          </el-form-item>
          <el-form-item label="关联合同" prop="templateId">
            <el-input
              v-model="this.contractTemplateData.templateName"
              placeholder="请选择合同"
              readonly
              style="width: 220px; margin-right: 10px"
            ></el-input>
            <el-button
              size="mini"
              type="primary"
              @click="dialogSelectContractVisible = true"
              >选择合同</el-button
            >
          </el-form-item>
        </div>

        <div class="section">
          <div class="label">
            <label>房屋配置</label>
          </div>
          <el-form-item label="房管家" prop="stewardPhone">
            <el-input
              v-model="form.stewardPhone"
              type="number"
              placeholder="请输入"
              style="width: 220px; margin-right: 5px"
            ></el-input>
          </el-form-item>
          <el-form-item label="装修情况" prop="decorationId">
            <el-select
              v-model="form.decorationId"
              placeholder="请选择"
              style="width: 220px"
            >
              <el-option
                v-for="(item, index) in decorationList"
                :key="index"
                :label="item.decorationName"
                :value="item.decorationId"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="房屋配置" required prop="facilitiesItems">
            <div class="facilities">
              <i
                class="icon el-icon-circle-plus"
                @click="showDialogFacilities"
              ></i>
              <el-row
                :gutter="10"
                class=""
                v-for="(item, index) in form.facilitiesItems"
                :key="item.facilitiesId"
                style="margin-bottom: 10px"
              >
                <el-col :span="8">
                  <div class="facilities-item">
                    <span class="item-label">{{ item.facilitiesName }}</span>
                    <el-input
                      v-input.num
                      placeholder="请输入数量"
                      v-model="item.facilitiesNumber"
                      style="width: 220px"
                    >
                    </el-input>
                  </div>
                </el-col>
                <el-col :span="8">
                  <div class="facilities-item">
                    <span class="item-label">单价</span>
                    <el-input
                      v-input.num_point
                      placeholder="请输入单价"
                      v-model="item.facilitiesPrice"
                      style="width: 220px"
                    >
                    </el-input>
                    <span style="margin: 0 10px">元</span>
                  </div>
                </el-col>
                <el-col :span="2">
                  <i
                    class="icon el-icon-remove"
                    @click="removeFacitieItem(index)"
                  ></i>
                </el-col>
              </el-row>
            </div>
          </el-form-item>
        </div>

        <el-form-item label="标签" prop="labelIdList">
          <el-row :gutter="10">
            <el-col :span="6">
              <el-select
                v-model="form.labelIdList"
                placeholder="请选择"
                multiple
              >
                <el-option
                  :label="item.labelName"
                  :value="item.labelId"
                  v-for="(item, index) in labelList"
                  :key="index"
                ></el-option>
              </el-select>
            </el-col>
            <el-col :span="6">
              <el-button
                type="text"
                @click="customLabelStatus = true"
                v-if="!customLabelStatus"
                >自定义标签</el-button
              >
              <el-input
                v-model="customLabelContent"
                placeholder="请输入标签内容"
                @blur="addCustomLabel"
                v-focus
                @keyup.enter.native="addCustomLabel"
                v-else
              ></el-input>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="描述" prop="apartmentDescription">
          <el-input
            type="textarea"
            v-model="form.apartmentDescription"
          ></el-input>
        </el-form-item>
        <div class="section">
          <div class="label">
            <label>周边配置</label>
            <!-- <i class="icon el-icon-circle-plus" @click="addConfig"></i> -->
          </div>
          <el-form-item label="景区" prop="attractionsIdList">
            <el-select
              v-model="form.attractionsIdList"
              placeholder="请选择景区"
              multiple
            >
              <el-option
                v-for="(item, index) in attractionsList"
                :key="index"
                :label="item.attractionsName"
                :value="item.attractionsId"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="商圈" prop="businessCircleIdList">
            <el-select
              v-model="form.businessCircleIdList"
              placeholder="请选择商圈"
              multiple
            >
              <el-option
                v-for="(item, index) in circleList"
                :key="index"
                :label="item.businessCircleName"
                :value="item.businessCircleId"
              ></el-option>
            </el-select>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">保 存</el-button>
      </div>
    </el-dialog>

    <!-- 新增价格日历弹窗 -->
    <el-dialog
      title="价格日历"
      :visible.sync="dialogPriceFormVisible"
      class="priceDialog"
    >
      <div class="section">
        <div class="label">
          <label class="dot">选择日期</label>
          <i class="icon el-icon-circle-plus" @click="addPriceCalendar"></i>
        </div>
      </div>
      <el-form :model="priceForm" :rules="priceFormRules" ref="priceForm">
        <div v-for="(item, index) in calendarPriceList" :key="index">
          <div class="form-container">
            <div class="prcieForm">
              <el-form-item label="日期时间" prop="time">
                <el-date-picker
                  v-model="item.startTime"
                  type="date"
                  placeholder="选择开始日期"
                  value-format="yyyy-MM-dd"
                  :picker-options="item.startTimeOptions"
                  @change="changePriceCalendar(index, 'startTime')"
                >
                </el-date-picker>
                至
                <el-date-picker
                  v-model="item.endTime"
                  type="date"
                  placeholder="选择结束日期"
                  value-format="yyyy-MM-dd"
                  :picker-options="item.endTimeOptions"
                  @change="changePriceCalendar(index, 'endTime')"
                >
                </el-date-picker>
              </el-form-item>
              <el-form-item label="价格" prop="price">
                <el-input
                  v-model="item.price"
                  placeholder="请输入"
                  v-input.float="2"
                  style="width: 220px; margin-right: 5px"
                ></el-input
                >元
              </el-form-item>
            </div>

            <i
              class="icon el-icon-remove"
              @click="removePriceCalendar(index)"
            ></i>
          </div>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogPriceFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCalendarPrice">确定</el-button>
      </div>
    </el-dialog>
    <!-- 房屋配置弹窗 -->
    <el-dialog
      title="房屋配置"
      :visible.sync="dialogFacilitiesVisible"
      class="facilitiesDialog"
      :before-close="closeFacilitiesDialog"
      :destroy-on-close="true"
    >
      <el-form
        :model="facilitiesForm"
        ref="facilitiesForm"
        class="facilitiesForm"
      >
        <el-form-item prop="facilitiesArr">
          <el-checkbox-group v-model="facilitiesForm.facilitiesArr">
            <el-checkbox
              v-for="(item, index) in facilitiesList"
              :label="item.facilitiesId"
              name="type"
              :key="index"
              >{{ item.facilitiesName }}</el-checkbox
            >
          </el-checkbox-group>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFacilitiesVisible = false">取 消</el-button>
        <el-button type="primary" @click="selectFacilities">保存</el-button>
      </div>
    </el-dialog>
    <price-calendar :show.sync="showCalendar" />
    <select-rule-dialog
      v-model="dialogSelectRuleVisible"
      @success="selectRuleCb"
    />
    <select-contract-template-dialog
      v-model="dialogSelectContractVisible"
      @success="selectContractCb"
    />
  </div>
</template>

<script>
import {
  listDecoration,
  listFacilities,
  listPayType,
  getHousingAllocation,
  listAttractions,
  listCircle,
  addHousingAllocation,
  updateHousingAllocation,
  getAllInfoLabel,
  listRange,
  addLabel,
} from "@/api/house/unmount";
import { getContractTemplate } from "@/api/contract/template";
import { getUnsubscribeRule } from "@/api/house/rule";
import calendar from "./calendar/index.vue";
import selectRuleDialog from "../../components/select-rule-dialog.vue";
import selectContractTemplateDialog from "../../components/select-contractTemplate-dialog.vue";
export default {
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    houseId: {
      default: undefined,
    },
    title: {
      default: "编辑",
    },
    success: {
      type: Function,
      default: () => {},
    },
  },
  components: {
    priceCalendar: calendar,
    selectRuleDialog,
    selectContractTemplateDialog,
  },
  data() {
    var checkFacilitiesItem = (rule, value, callback) => {
      value.forEach((item) => {
        if (!item.facilitiesNumber || !item.facilitiesPrice) {
          callback(new Error(`${item.facilitiesName}的数量或单价不能为空`));
        }
      });

      if (!value) {
        return callback(new Error("年龄不能为空"));
      }
      callback();
    };
    var checkPriceMap = (rule, value, callback) => {
      if (!this.priceMap.length) callback(new Error(`收费标准不能为空`));
      this.priceMap.forEach((item) => {
        if (!item.timeRangeId || !item.fixedPrice) {
          callback(new Error(`时效收费相关数据不能为空`));
        }
      });
      callback();
    };
    return {
      visible: false,
      value1: "",
      //退订规则数据
      unsubscribeRuleData: {},
      //合同数据
      contractTemplateData: {},
      //房屋数据
      houseData: {},
      //装修状况列表
      decorationList: [],
      //押付方式列表
      payTypeList: [],
      // 商圈列表
      circleList: [],
      // 景点列表
      attractionsList: [],
      //标签列表
      labelList: [],
      //价格区间列表
      rangeList: [],

      dialogFacilitiesVisible: false,
      facilitiesList: [],
      // facilitieSelected: [],
      facilitiesForm: {
        facilitiesArr: [],
      },
      form: {},
      configList: [],
      dialogPriceFormVisible: false,
      priceForm: {},
      calendarPriceList: [],
      //时效收费list
      priceMap: [],
      showCalendar: false,
      dialogSelectRuleVisible: false,
      dialogSelectContractVisible: false,
      customLabelStatus: false,
      customLabelContent: "",
      rules: {
        apartmentPicture: [
          { required: true, message: "请上传图片", trigger: "blur" },
        ],
        apartmentType: [
          { required: true, message: "请选择周期类型", trigger: "blur" },
        ],
        rentStartingTime: [
          { required: true, message: "请输入起租时长", trigger: "blur" },
        ],
        rentableStartTime: [
          {
            required: true,
            message: "请选择日期",
            trigger: "blur",
          },
        ],
        rentableEndTime: [
          {
            required: true,
            message: "请选择日期",
            trigger: "blur",
          },
        ],
        noRentableStartTime: [
          {
            required: true,
            message: "请选择日期",
            trigger: "blur",
          },
        ],
        noRentableEndTime: [
          {
            required: true,
            message: "请选择日期",
            trigger: "blur",
          },
        ],
        checkoutTime: [
          {
            required: true,
            message: "请选择可入住日期",
            trigger: "blur",
          },
        ],
        leaseEndTime: [
          {
            required: true,
            message: "请选择退租截止日期日期",
            trigger: "blur",
          },
        ],
        intervalTime: [
          {
            required: true,
            message: "请输入间隔天数",
            trigger: "blur",
          },
        ],
        leaseRemindTime: [
          {
            required: true,
            message: "请输入提前天数",
            trigger: "blur",
          },
        ],
        chargeType: [
          { required: true, message: "请选择收费类型", trigger: "blur" },
        ],
        priceMap: [{ validator: checkPriceMap, trigger: "blur" }],
        fixedPrice: [
          { required: true, message: "请输入固定租金", trigger: "blur" },
        ],
        averageDaily: [
          { required: true, message: "请输入日租金", trigger: "blur" },
        ],
        monthPrice: [
          { required: true, message: "请输入月租金", trigger: "blur" },
        ],
        cashPledge: [
          { required: true, message: "请输入押金", trigger: "blur" },
        ],
        servicePrice: [
          { required: true, message: "请输入服务费", trigger: "blur" },
        ],
        // baojie: [{ required: true, message: "请输入保洁费", trigger: "blur" }],
        unsubscribeRuleId: [
          { required: true, message: "请选择退订规则", trigger: "blur" },
        ],
        templateId: [
          { required: true, message: "请选择关联合同", trigger: "blur" },
        ],
        stewardPhone: [
          { required: true, message: "请输入房管家电话", trigger: "blur" },
        ],
        decorationId: [
          { required: true, message: "请选择装修情况", trigger: "blur" },
        ],
        facilitiesItems: [
          { required: true, message: "房屋配置不能为空", trigger: "blur" },
          { validator: checkFacilitiesItem, trigger: "blur" },
        ],
        labelIdList: [
          { required: true, message: "请输入标签", trigger: "blur" },
        ],
        apartmentDescription: [
          { required: true, message: "请输入描述", trigger: "blur" },
        ],
      },
      priceFormRules: {
        time: [
          {
            validator: (rule, value, callback) => {
              if (!this.calendarPriceList.length)
                callback(new Error(`请添加日期`));
              this.calendarPriceList.forEach((item) => {
                if (!item.startTime || !item.endTime) {
                  callback(new Error(`开始日期和结束日期不能为空`));
                }
              });
              callback();
            },
          },
        ],
        price: [
          {
            validator: (rule, value, callback) => {
              if (!this.calendarPriceList.length)
                callback(new Error(`请添加日期`));
              this.calendarPriceList.forEach((item) => {
                if (!item.price) {
                  callback(new Error(`价格不能为空`));
                }
              });
              callback();
            },
          },
        ],
      },
      // 可租时间选择配置
      rentableStartTimeOptions: {
        disabledDate: (time) => {
          let endTime = new Date(this.form["rentableEndTime"]).getTime();
          return time.getTime() > endTime;
        },
      },
      rentableEndTimeOptions: {
        disabledDate: (time) => {
          let startTime = new Date(this.form["rentableStartTime"]).getTime();
          return time.getTime() < startTime;
        },
      },
      //不可租时间选择配置
      noRentableStartTimeOptions: {
        disabledDate: (time) => {
          let endTime = new Date(this.form["noRentableEndTime"]).getTime();
          return time.getTime() > endTime;
        },
      },
      noRentableEndTimeOptions: {
        disabledDate: (time) => {
          let startTime = new Date(this.form["noRentableStartTime"]).getTime();
          return time.getTime() < startTime;
        },
      },
    };
  },
  computed: {
    generateHouseName() {
      return (row) => {
        let name = "";
        if (row.provinceName) name += row.provinceName;
        if (row.cityName) name += row.cityName;
        if (row.districtName) name += row.districtName;
        if (row.streetName) name += row.streetName;

        if (row.estateName) name += row.estateName;
        if (row.buildingName) name += row.buildingName + "栋";
        if (row.unitName) name += row.unitName + "单元";
        if (row.roomNumber) name += row.roomNumber + "号";
        return name;
      };
    },
  },
  watch: {
    value(newVal, oldVal) {
      if (newVal) {
        this.init();
      }
      this.visible = newVal; // 监测value的变化，并赋值。
    },
    visible(val) {
      this.$emit("input", val); // 此处监测visible目的为关闭弹窗时，重新更换value值，注意emit的事件一定要为input。
    },
  },
  methods: {
    //初始化
    init() {
      this.getListDecoration();
      this.getListCircle();
      this.getListPayType();
      this.getListAttractions();
      this.getAllInfoLabel();
      this.getListRange();
      this.reset();
      //获取详情信息
      this.getHousingAllocation();
    },
    //新增周边配置
    addConfig() {
      let data = {
        type: "",
        content: "",
      };
      this.configList.push(data);
    },
    //选择退订规则回调
    selectRuleCb(data) {
      this.form.unsubscribeRuleId = data.unsubscribeRuleId;
      this.unsubscribeRuleData = data;
    },
    //获取退订规则详情
    getRule() {
      if (!this.form.unsubscribeRuleId) return;
      let id = this.form.unsubscribeRuleId;
      getUnsubscribeRule(id).then((response) => {
        this.unsubscribeRuleData = response.data;
      });
    },
    //选择合同回调
    selectContractCb(data) {
      this.form.templateId = data.templateId;
      this.contractTemplateData = data;
    },
    //获取合同详情
    getContractTemplate() {
      if (!this.form.templateId) return;
      let id = this.form.templateId;
      getContractTemplate(id).then((response) => {
        this.contractTemplateData = response.data;
      });
    },
    //价格日历新增数据
    addPriceCalendar() {
      let data = {
        startTime: "",
        endTime: "",
        price: null,
        startTimeOptions: null,
        endTimeOptions: null,
      };
      this.calendarPriceList.push(data);
    },
    //获取装修状况
    getListDecoration() {
      listDecoration()
        .then((res) => {
          if (res.code == 200) {
            this.decorationList = res.data;
          }
        })
        .catch((err) => {});
    },
    //获取押付方式列表
    getListPayType() {
      listPayType().then((response) => {
        if (response.code == 200) {
          this.payTypeList = response.data;
        }
      });
    },
    //获取商圈
    getListCircle() {
      listCircle().then((response) => {
        if (response.code == 200) {
          this.circleList = response.data;
        }
      });
    },
    //获取景点
    getListAttractions() {
      listAttractions().then((response) => {
        if (response.code == 200) {
          this.attractionsList = response.data;
        }
      });
    },
    //获取标签
    getAllInfoLabel() {
      getAllInfoLabel().then((response) => {
        if (response.code == 200) {
          this.labelList = response.data;
        }
      });
    },
    //添加自定义标签
    addCustomLabel() {
      let value = this.customLabelContent;
      if (!value) {
        this.customLabelStatus = false;
        return;
      }

      let data = {
        labelName: value, //标签名
      };
      addLabel(data).then((response) => {
        if (response.code == 200) {
          this.customLabelContent = "";
          this.getAllInfoLabel();
          this.$nextTick(() => {
            this.form.labelIdList.push(response.data.labelId);
          }, 500);
          this.customLabelStatus = false;
        }
      });
    },
    //获取价格区间
    getListRange() {
      listRange().then((response) => {
        if (response.code == 200) {
          this.rangeList = response.data;
        }
      });
    },
    //展示房屋配置弹窗
    showDialogFacilities() {
      listFacilities().then((res) => {
        if (res.code == 200) {
          this.facilitiesList = res.data;
          this.facilitiesForm.facilitiesArr = [];
          this.dialogFacilitiesVisible = true;
        }
      });
    },
    closeFacilitiesDialog(done) {
      this.$refs.facilitiesForm.resetFields();
      this.facilitiesForm.facilitiesArr = [];
      done();
    },
    // 选择设施
    selectFacilities() {
      let list = this.facilitiesList.filter((item) =>
        this.facilitiesForm.facilitiesArr.includes(item.facilitiesId)
      );
      this.form.facilitiesItems = list.map((item) => {
        return {
          facilitiesId: item.facilitiesId,
          facilitiesName: item.facilitiesName,
          facilitiesPrice: item.facilitiesPrice,
          facilitiesNumber: undefined,
        };
      });
      this.form.facilitiesItems = this.form.facilitiesItems;
      this.dialogFacilitiesVisible = false;
    },
    //移除设施项
    removeFacitieItem(index) {
      this.form.facilitiesItems.splice(index, 1);
    },
    //展示价格日历
    showCalendarPrice() {
      let calendarPriceList = this.form.calendarPriceList
        ? JSON.parse(JSON.stringify(this.form.calendarPriceList))
        : [];

      for (let index = 0; index < calendarPriceList.length; index++) {
        calendarPriceList[index]["endTimeOptions"] = {
          disabledDate: (time) => {
            let startTime = new Date(
              calendarPriceList[index].startTime
            ).getTime();
            return time.getTime() < startTime;
            // return (
            //   time.getTime() < startTime ||
            //   time.getTime() < Date.now()
            // );
          },
        };
        calendarPriceList[index]["startTimeOptions"] = {
          disabledDate: (time) => {
            let endTime = new Date(calendarPriceList[index].endTime).getTime();
            return time.getTime() > endTime;
          },
        };
      }
      this.calendarPriceList = calendarPriceList;
      this.dialogPriceFormVisible = true;
    },
    //添加收费标准字段
    addPriceMap() {
      this.priceMap.push({
        timeRangeId: null,
        fixedPrice: null,
      });
    },
    removePriceMap(index) {
      this.priceMap.splice(index, 1);
    },
    //确认价格日历
    addCalendarPrice() {
      this.$refs["priceForm"].validate((valid) => {
        if (valid) {
          let calendarPriceList = JSON.parse(
            JSON.stringify(this.calendarPriceList)
          );
          delete calendarPriceList["startTimeOptions"];
          delete calendarPriceList["endTimeOptions"];
          this.form.calendarPriceList = calendarPriceList;
          this.dialogPriceFormVisible = false;
        }
      });
    },
    //移除项价格日历项
    removePriceCalendar(index) {
      this.calendarPriceList.splice(index, 1);
    },
    changePriceCalendar(index, type) {
      if (type == "startTime") {
        let startTime = new Date(
          this.calendarPriceList[index].startTime
        ).getTime();
        this.calendarPriceList[index]["endTimeOptions"] = {
          disabledDate: (time) => {
            return time.getTime() < startTime;
          },
        };
      }
      if (type == "endTime") {
        let endTime = new Date(this.calendarPriceList[index].endTime).getTime();
        // console.log('startDisabled',endTime);
        this.calendarPriceList[index]["startTimeOptions"] = {
          disabledDate: (time) => {
            return time.getTime() > endTime;
          },
        };
      }
      this.$forceUpdate();
    },
    //重置表单
    reset() {
      this.unsubscribeRuleData = {};
      this.contractTemplateData = {};
      this.houseData = {};
      this.priceMap = [];
      this.customLabelContent = "";
      this.customLabelStatus = false;
      this.form = {
        allocationId: undefined, //房屋配置id
        houseId: "", //房屋id
        apartmentPicture: "", //房屋照片路径
        apartmentType: "", //公寓类型 1短租公寓 2长租公寓
        rentStartingTime: undefined, //最小起租时间
        noRentableStartTime: "", //不可租开始时间
        noRentableEndTime: "", //不可租结束时间
        checkoutTime: "", //短租可入住时间
        leaseEndTime: "", //短租退租截止时间
        chargeType: "", //收费类型：0固定收费 1时效收费
        fixedPrice: undefined, //固定收费价格
        cashPledge: undefined, //押金
        servicePrice: undefined, //服务费
        unsubscribeRuleId: undefined, //退订规则id
        templateId: undefined, //合同id
        stewardPhone: "", //管家电话
        decorationId: undefined, //装修情况id
        facilitiesItems: [], //房屋配置明细
        labelIdList: [], //公寓标签ids
        apartmentDescription: "", //房屋描述
        attractionsIdList: [], //景区id集合
        businessCircleIdList: [], //商区id集合
        agingPriceMap: {}, //时效收费价格接参
        averageDaily: undefined, //日均价
        monthPrice: undefined, //月租价
        payTypeId: undefined, //押付方式id
        rentableStartTime: "", //可租开始时间
        rentableEndTime: "", //可租结束时间
        calendarPriceList: [], //价格日历集合
      };
      this.resetForm("form");
    },
    //获取详情信息
    getHousingAllocation() {
      if (!this.houseId) return;
      getHousingAllocation(this.houseId)
        .then((response) => {
          for (const key in this.form) {
            if (Object.hasOwnProperty.call(this.form, key)) {
              this.form[key] = response.data[key];
              //房屋id
              if (key == "houseId") {
                this.form[key] = response.data.house.houseId;
              }
            }
          }
          //房屋数据渲染
          this.houseData = response.data.house;
          //价格区间渲染
          this.form.agingPriceMap &&
            Object.keys(this.form.agingPriceMap).forEach((item) => {
              this.priceMap.push({
                timeRangeId: Number(item),
                fixedPrice: this.form.agingPriceMap[item],
              });
            });
          //标签渲染
          let labelIdsArr = response.data.labelIds
            .split(",")
            .map((item) => Number(item));
          this.form.labelIdList = labelIdsArr;
          this.getRule();
          this.getContractTemplate();
        })
        .catch((err) => {});
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let agingPriceMap = {};
          if (this.priceMap.length) {
            this.priceMap.map((item) => {
              agingPriceMap[item.timeRangeId] = item.fixedPrice;
            });
            this.form.agingPriceMap = agingPriceMap;
          }
          // console.log('agingPriceMap',agingPriceMap);return
          if (this.form.allocationId != undefined) {
            updateHousingAllocation(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.$emit("success");
            });
          } else {
            addHousingAllocation(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.$emit("success");
            });
          }
          this.close();
        }
      });
    },
    //关闭
    close() {
      this.reset();
      this.visible = false;
    },
  },
};
</script>

<style lang="scss" scoped>
//弹出层的高度
.import-dialog {
  height: auto;
  max-height: 80vh;
  overflow-y: auto;
  .header {
    .title {
      background: #ceeefc;
      padding: 10px 30px;
      margin-bottom: 30px;
      font-weight: 700;
      font-size: 20px;
    }
  }
  .demo-form {
    .line {
      text-align: center;
    }
  }
  .expense {
    color: #02a7f0;
    .icon {
      font-size: 30px;
      margin-right: 5px;
      cursor: pointer;
    }
    .price-handle {
      margin: 0 40px;
      cursor: pointer;
    }
    .calendar {
      cursor: pointer;
    }
  }
  .facilities {
    .icon {
      color: #02a7f0;
      font-size: 30px;
      margin-right: 5px;
      cursor: pointer;
      line-height: 36px;
    }
    .facilities-item {
      display: flex;
      .item-label {
        text-align: right;
        margin-right: 10px;
      }
    }
  }
  .dialog-footer {
    text-align: center;
  }
}
//弹出层里内容的高度
::v-deep .el-dialog__body {
  max-height: 80vh !important;
  overflow-y: auto;
}

.section {
  .label {
    display: flex;
    margin-bottom: 30px;
    font-size: 16px;
    align-items: center;
    label {
      display: block;
      width: 80px;
      text-align: right;
      &.dot::before {
        content: "*";
        color: #ff4949;
        margin-right: 4px;
      }
    }
    .icon {
      color: #02a7f0;
      font-size: 30px;
      margin-left: 30px;
      cursor: pointer;
    }
  }
  .tips {
    color: #aaaaaa;
  }
}

.priceDialog {
  .form-container {
    display: flex;
    justify-content: space-around;
    align-items: center;
    .prcieForm {
      border: 1px solid #e3e3e3;
      padding: 30px 20px;
      margin-bottom: 30px;
      flex: 1;
      margin-right: 10px;
    }
    .icon {
      color: #02a7f0;
      font-size: 30px;
      cursor: pointer;
    }
  }

  .dialog-footer {
    text-align: center;
  }
}

.facilitiesDialog {
  .dialog-footer {
    text-align: center;
  }
}
</style>
